#import "Utils.h"
#import "AppDelegate.h"

//#import "THWUserInfoModel.h"


#define ERROR @"error"
#define TipMsg @"提示"
#define OkBtnMsg @"确定"


/**
 *  <#Description#>
*/
#define MSGKey @"message"
#define MSGType @"type"
#define MSGContent @"content"
#define SUCCESS @"success"
#define ERROR @"error"
#define INVALID @"session.invaild"

static NSMutableDictionary *modelStateMutableDic;

@implementation Utils

#pragma  请求数据处理函数


/**
 *  获取数据返回状态
 *
 *  @param responDic 数据请求返回的字典
 *  @param msg       显示提示内容
 *
 *  @return 数据请求状态
 */
+ (SESSIONSTATE) getSesstionState:(NSDictionary *)responDic
{
    SESSIONSTATE state;
    NSString *msg =responDic[MSGKey][MSGType];
    if([msg isEqualToString:SUCCESS])
    {
        state =  SESSIONSUCCESS;
    }
    else if([msg isEqualToString:ERROR])
    {
         state =  SESSIONERROR;
    }
    else if([msg isEqualToString:INVALID])
    {
        state =  SESSIONINVALID;
    }
    return state;
}



/**
 *  获取接口数据
 *
 *  @param responDic 数据请求返回的字典
 *
 *  @return 数组（字典构成）
 */
+ (NSDictionary*) getData:(NSDictionary *)responDic
{
    NSDictionary * data = responDic[@"data"];
    return data;
}


/**
 *  将字典转换为JSON
 *
 *  @param dictModel <#dictModel description#>
 *
 *  @return <#return value description#>
 */
+(NSString *)JsonModel:(NSDictionary *)dictModel
{
    if ([NSJSONSerialization isValidJSONObject:dictModel])
    {
        NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dictModel options:NSJSONWritingPrettyPrinted error:nil];
        NSString * jsonStr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        return jsonStr;
    }
    return nil;
}

+(void) setLocation:(CGFloat) latitude lngtibute:(CGFloat) longitude
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setDouble:latitude forKey:@"lat"];
    [userDefaults setDouble:longitude forKey:@"lng"];
}

+(void) setCityName:(NSString *)cityName
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:cityName forKey:@"cityName"];
}

+(NSString *)getCityName
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults  objectForKey:@"cityName"];
}


+(id) getLocationLat
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults  objectForKey:@"lat"]?:@"";
}

+(id) getLocationLng
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults  objectForKey:@"lng"]?:@"";
}

/**
 *  获取登录状态
 *
 */
+ (void)setUserToken:(NSString *)userToken
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:userToken forKey:@"userToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


+ (NSString *)getUserToken
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:@"userToken"]?[userDefaults objectForKey:@"userToken"]:@"";
}

//获取经纬度
+ (void)setJWD:(NSString *)j and:(NSString *)w
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[NSString stringWithFormat:@"%@,%@", j, w] forKey:@"JWD"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)getJWD
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:@"JWD"];
}

//获取搜索数组
+ (NSMutableArray *)getSearchArr
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:@"SearchArr"];
}

+ (void)SetSearchArr:(NSMutableArray *)SearchArr
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:SearchArr forKey:@"SearchArr"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(UIImageView *)getStar:(NSString *)grade
{
    UIImageView *starImg=[UIImageView new];
    if ([String(grade) isEqualToString:@"1"]) {
        [starImg setImage:[UIImage imageNamed:@"GoodsDetailOneStar"]];
    }else if ([String(grade) isEqualToString:@"2"]) {
        [starImg setImage:[UIImage imageNamed:@"GoodsDetailTwoStar"]];
    }else if ([String(grade) isEqualToString:@"3"]) {
        [starImg setImage:[UIImage imageNamed:@"GoodsDetailThreeStar"]];
    }else if ([String(grade) isEqualToString:@"4"]) {
        [starImg setImage:[UIImage imageNamed:@"GoodsDetailFourStar"]];
    }else if ([String(grade) isEqualToString:@"5"]) {
        [starImg setImage:[UIImage imageNamed:@"GoodsDetailFiveStar"]];
    }
    return starImg;
}

/**
 *  将图片转换为Data
 *
 *  @param img <#img description#>
 *
 *  @return <#return value description#>
 */
+(NSData*) converImgToData:(UIImage *)img
{
    NSData *data;
    if (UIImagePNGRepresentation(img) == nil) {
        
        data = UIImageJPEGRepresentation(img, 1);
        
    } else {
        
        data = UIImagePNGRepresentation(img);
    }
    return data;
}


/**
 *  打电话
 *
 *  @param img <#img description#>
 *
 *  @return <#return value description#>
 */
+(void)phone:(NSString *)mobile controller:(UIViewController *)controller
{
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",mobile];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [controller.view addSubview:callWebview];
}

+ (UISearchBar *)createSearchBar:(CGFloat)x and:(CGFloat)y and:(CGFloat)width
{
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(x, y, width, 30)];
    searchBar.backgroundColor = [UIColor clearColor];
    searchBar.showsCancelButton = NO;
    searchBar.placeholder = @"";
    
    for (UIView *subView in searchBar.subviews) {
        if ([subView isKindOfClass:[UIView  class]]) {
            [[subView.subviews objectAtIndex:0] removeFromSuperview];
            if ([[subView.subviews objectAtIndex:0] isKindOfClass:[UITextField class]]) {
                UITextField *textField = [subView.subviews objectAtIndex:0];
                textField.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
                
                //设置默认文字颜色
                UIColor *color = [UIColor grayColor];
                [textField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"搜索商品/店铺" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:color}]];
                //修改默认的放大镜图片
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 13, 13)];
                imageView.backgroundColor = [UIColor clearColor];
                imageView.image = [UIImage imageNamed:@"LineDownSearch"];
                textField.leftView = imageView;
            }
        }
    }
    return searchBar;
}

/**
 同步代码到磁盘

 @return <#return value description#>
 */
+(BOOL)synchronizeToDisk
{
    return [[NSUserDefaults standardUserDefaults] synchronize];
}


/**
 保存图片到相册中

 @param urlKey <#urlKey description#>
 */
+(void) saveImgsForPhotoAlbum:(NSData *)data
{
//    NSURL *url = [NSURL URLWithString:urlKey];
//    SDWebImageManager *manager = [SDWebImageManager sharedManager];
      UIImage *img;
//    if ([manager diskImageExistsForURL:url])
//    {
//        img =  [[manager imageCache] imageFromDiskCacheForKey:urlKey];
//        
//    }
//    else
//    {
//        //从网络下载图片
//        NSData *data = [NSData dataWithContentsOfURL:url];
        img = [UIImage imageWithData:data];
//    }
    // 保存图片到相册中
    UIImageWriteToSavedPhotosAlbum(img,self,nil,nil);
}


/**
 拷贝字符串到剪切板

 @param string <#string description#>
 @return <#return value description#>
 */
+(void) copyToPosteBoard:(NSString *)string cleanText:(BOOL) enable
{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    if(enable){
        pasteboard.string=@"";
    }
    else
    {
        if(!pasteboard.string)
            pasteboard.string=@"";
    }
    pasteboard.string =[NSString stringWithFormat:@"%@%@",pasteboard.string,string];
}


/**
 获取当前APP的名称

 @return <#return value description#>
 */
+(NSString *)boundleDisplayName
{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:bundlePath];
    return  [dict objectForKey:@"CFBundleDisplayName"];
}

+ (NSString *)getImaSting:(NSString *)str
{
    return [NSString stringWithFormat:@"http://www.rmgs9.com/%@", str];
}
@end
